package com.dfk.bs.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dfk.bs.common.constants.ResultCodeEnum;
import com.dfk.bs.common.exception.BsException;
import com.dfk.bs.common.vo.R;
import com.dfk.bs.entity.Eleapp;
import com.dfk.bs.entity.Housetype;
import com.dfk.bs.entity.Room;
import com.dfk.bs.service.EleappService;
import com.dfk.bs.service.HousetypeService;
import com.dfk.bs.service.RoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 房间表 前端控制器
 * </p>
 *
 * @author DDK
 * @since 2019-12-12
 */

@Api("房屋类型管理")
@CrossOrigin
@RestController
@RequestMapping("/ddkbishe/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @Autowired
    private EleappService eleappService;


    @ApiOperation("所有房间列表")
    @GetMapping("/list")
    public R list(){

        List<Room> list = roomService.list(null);

        return R.ok()
                .message("查询房屋列表")
                .data("items",list);
    }

    @ApiOperation("新增房间")
    @PostMapping("/save")
    public R save(
            @ApiParam(name="room",value = "房间信息",required = true)
            @RequestBody Room room){
        roomService.save(room);
        return R.ok()
                .message("新增房屋类型成功");
    }

    @ApiOperation("根据房间id删除房间")
    @DeleteMapping("/remove/{roomid}")
    public R removeById(
            @ApiParam(name = "roomid",value = "房间id", required = true)
            @PathVariable String roomid){
        roomService.removeById(roomid);
        return R.ok()
                .message("删除房屋类型成功");
    }

    @ApiOperation("分页条件查询房间列表")
    @PostMapping("/pageQuery")
    public R pageQuery(@RequestBody Map<String, Object> params){

        Integer page = (Integer) params.get("page");
        Integer limit = (Integer) params.get("limit");

        String selectname = (String) params.get("selectname");

        //业务逻辑错误，抛出自定义异常
        if(page <= 0 || limit <= 0){
            throw new BsException(ResultCodeEnum.PARAM_ERROR);
        }
        Page<Room> pageParam=new Page<>(page,limit);

        /*已经把查询的每一页的信息放到 pageParam中*/
        roomService.pageQuery(pageParam,selectname);

        List<Eleapp> eleappList = eleappService.list();

        List<Room> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return R.ok()
                .message("分页条件查询房间列表成功")
                .data("total",total)
                .data("rows",records)
                .data("rows2",eleappList);
    }

    @ApiOperation(value = "根据ID修改房间信息")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(name = "room", value = "房间对象", required = true)
            @RequestBody Room room){
        roomService.updateById(room);
        return R.ok()
                .message("修改房间信息成功");
    }

}

